package org.blackjuly.sign.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.blackjuly.sign.data.DataBean;
import org.blackjuly.sign.entity.Chart;

public class ChartService extends BaseService {
	/**
	 * 根据学生名字获取排名 student 主键值
	 */
	public Chart findGradeByStudent(String student) throws Exception {
		DataBean<Chart> dataBean = new DataBean<>(conn, "reaking", "name");
		dataBean.setEntityClass(Chart.class);
		return dataBean.doSelect(student);
	}

	/**
	 * 根据学生id获取对象 tablename 主键值 , id 查找id
	 */
	public Chart findGradeById(int id, String tablename) throws Exception {
		DataBean dataBean = new DataBean(this.conn, tablename, "id");
		dataBean.setEntityClass(Chart.class);
		return (Chart) dataBean.doSelect(Integer.valueOf(id));
	}

	/**
	 * 获取最大的学生，往上的百数
	 */
	public int[] getMaxGrade(Chart chart) throws Exception {
		List list = new ArrayList();
		list.add(Integer.valueOf(chart.getR1()));
		list.add(Integer.valueOf(chart.getR2()));
		list.add(Integer.valueOf(chart.getR3()));
		list.add(Integer.valueOf(chart.getR4()));
		list.add(Integer.valueOf(chart.getR5()));
		list.add(Integer.valueOf(chart.getR6()));
		list.add(Integer.valueOf(chart.getR7()));
		list.add(Integer.valueOf(chart.getR8()));
		list.add(Integer.valueOf(chart.getR9()));
		list.add(Integer.valueOf(chart.getR10()));
		list.add(Integer.valueOf(chart.getR11()));
		list.add(Integer.valueOf(chart.getR12()));
		list.add(Integer.valueOf(chart.getR13()));
		list.add(Integer.valueOf(chart.getR14()));
		list.add(Integer.valueOf(chart.getR15()));
		list.add(Integer.valueOf(chart.getR16()));
		list.add(Integer.valueOf(chart.getR17()));
		int m = 0;
		int y = 0;
		for (int i = 0; i < list.size(); i++) {
			if (((Integer) list.get(i)).intValue() == 0) {
				y += 1;
			} else
				m = ((Integer) list.get(i)).intValue() + m;
		}
		Collections.sort(list);
		double a = ((Integer) list.get(list.size() - 1)).intValue() / 100.0D;
		BigDecimal aa = new BigDecimal(a);
		double s = aa.setScale(0, 0).doubleValue() * 100.0D;
		int[] b = { (int) s, m / (list.size() - y) };
		return b;
	}
}